package yzbx.personalwebsite.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import yzbx.personalwebsite.config.ConfigData;
import yzbx.personalwebsite.dao.CommonDao;
import yzbx.personalwebsite.entity.UserEntity;
import yzbx.personalwebsite.entityWeb.HttpResponse;
import yzbx.personalwebsite.entityWeb.SessionRequst;
import yzbx.personalwebsite.service.CommonService;
import yzbx.personalwebsite.tool.utilTool;

import java.util.Map;

import static yzbx.personalwebsite.config.Constants.*;

/**
 * {@code @Author} zxm
 * {@code @Description} 用户处理类
 * {@code @Date} 2024/10/08/14:40
 * {@code @Version} 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    private final CommonService commonService;

    private final ConfigData configData;

    @Autowired
    public UserController(CommonDao commonDao, CommonService commonService, ConfigData configData) {
        this.commonService = commonService;
        this.configData = configData;
    }

    @RequestMapping("/sign")
    @ResponseBody
    public HttpResponse sign(@RequestParam(value = "file", required = false) MultipartFile file,
                             @RequestParam("id") String id,
                             @RequestParam("name") String name,
                             @RequestParam("password") String password) {
        if (file == null || file.isEmpty() || file.getOriginalFilename() == null) {
            LOGGER.info("文件为空");
            return new HttpResponse(HTTP_CODE_511, HTTP_CODE_511_MSG, null);
        }

        String fileName = name + utilTool.getRandomId(8) + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        utilTool.saveImg(file, configData.getTxPath(), fileName);

        UserEntity user = new UserEntity();
        user.setId(id);
        user.setName(name);
        user.setPassword(password);
        user.setTx(fileName);
        return commonService.sign(user);
    }

    @RequestMapping("/login")
    @ResponseBody
    public HttpResponse login(@RequestBody() UserEntity user) {
        return commonService.login(user);
    }

    @RequestMapping("/getUserInfo")
    @ResponseBody
    public HttpResponse getUserInfo(@RequestBody() SessionRequst sessionRequst) {
        UserEntity userEntity = commonService.getUserInfo(sessionRequst.getSession());
        if (userEntity == null) {
            return new HttpResponse(HTTP_CODE_504, HTTP_CODE_504_MSG, null);
        }
        return HttpResponse.success(userEntity);
    }

    @RequestMapping("/exit")
    @ResponseBody
    public HttpResponse exit(@RequestBody() Map<String, String> map) {
        return HttpResponse.success();
    }


}
